package com.mexcad.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mexcad.db.delegate.DelegateConnectors;
import com.mexcad.db.dto.Usuario;
import com.mexcad.utils.Formatter;


 public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

   
    /* (non-Java-doc)
 * @see javax.servlet.http.HttpServlet#HttpServlet()
 */
public LoginServlet() {
	super();
}   	

/* (non-Java-doc)
 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	HttpSession sessionHTTP = request.getSession();
	PrintWriter out;
	String login =request.getParameter("login");
	String contrasena =request.getParameter("passwd");
	///String query= "";
	Usuario usr = DelegateConnectors.consultaUsuario(login, contrasena);
	
	response.setContentType("text/html");
	request.setCharacterEncoding("UTF-8");
	
	out = response.getWriter();
	
	//cadena  = "{total:10, data: [ {anio: '10', id:'mica', partida:46.5, descripcion: 'hola'}]}";
	if( usr != null){
		
		sessionHTTP.setAttribute("usrLog", usr.getCkUsuario().getIdUsuario());
		sessionHTTP.setAttribute("usrNombreCompleto", usr.getNombres()+" "+usr.getApellidoPaterno()+" " +usr.getApellidoMaterno());
		sessionHTTP.setAttribute("tipoUsr", usr.getCkUsuario().getIdTipoUsuario());
		sessionHTTP.setAttribute("fechaAct", Formatter.getDateToday());
		sessionHTTP.setMaxInactiveInterval(1800); // 30 minutos = 60 segundos x 30 = 1800
		
		//out.print("true?element="+usr.getCkUsuario().getIdUsuario());

	//	response.sendRedirect();	
		out.println(response.encodeUrl("principal.jsp"));

	}else{
		out.print(response.encodeUrl("index.html"));
	}
	out.close(); 
}  	

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub

	}
}
